For the past few months, I’ve been learning about Swift coding. I’m relatively new to the coding field so this may seem trivial to some.
Here’s what I’m trying to do: I cannot get the data saved from regular textboxes to appear in the table I've designed on the storyboard. It is clearly saving something to the table rows but I cannot see it.
I've looked all over the web but most examples are from old versions of swift/deprecated versions of Xcode and are not applicable.
Basically, I’m designing an app for my company that allows quick and easy saving of users that call; I would be able to save their company, name, phone, userid, etc, the app saves it into the table And allows me to reference it later.
I'm using core data and I’ve attached all of the code to the appropriate storyboard fields.
any insight or errors someone could point out would be very helpful.
Here’s my code:
import Cocoa
import SwiftData
import SwiftUI
class User: NSManagedObject, Identifiable {
let id = UUID() //compatibility
@NSManaged public var company: String
@NSManaged public var name: String
@NSManaged public var phone: String
@NSManaged public var uid: String
@NSManaged public var cid: String
@NSManaged public var tvid: String
@NSManaged public var tvpwd: String
@NSManaged public var notes: String
}
class ViewController: NSViewController, NSTableViewDataSource, NSTableViewDelegate {
@IBOutlet weak var companyTextField: NSTextField!
@IBOutlet weak var nameTextField: NSTextField!
@IBOutlet weak var phoneTextField: NSTextField!
@IBOutlet weak var uidTextField: NSTextField!
@IBOutlet weak var cidTextField: NSTextField!
@IBOutlet weak var tvidTextField: NSTextField!
@IBOutlet weak var tvpwdTextField: NSTextField!
@IBOutlet weak var notesTextField: NSTextField!
@IBOutlet weak var tableView: NSTableView!
var users = [User]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
}
@IBAction func saveButtonClicked(_ sender: NSButton) {
let user = User()
users.append(user)
tableView.reloadData()
}
// MARK: - NSTableViewDataSource
func numberOfRows(in tableView: NSTableView) -> Int {
return users.count
}
// MARK: - NSTableViewDelegate
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
let user = users[row]
guard let cell = tableView.makeView(withIdentifier: tableColumn!.identifier, owner: self) as? NSTableCellView else { return nil }
switch tableColumn?.identifier.rawValue {
case "company":
cell.textField?.stringValue = user.company
case "name":
cell.textField?.stringValue = user.name
case "phone":
cell.textField?.stringValue = user.phone
case "uid":
cell.textField?.stringValue = user.uid
case "cid":
cell.textField?.stringValue = user.cid
case "tvid":
cell.textField?.stringValue = user.tvid
case "tvpwd":
cell.textField?.stringValue = user.tvpwd
case "notes":
cell.textField?.stringValue = user.notes
default:
return nil
}
return cell
}
}
![]